<?php

class Backend {

    private static $_instance;

    /**
     * 
     * @return Mongo_Mysql
     * 
     */
	public static function findUserById($id){
        $arr = array("_id" => $id);
        return self::factory()->find("users", $arr);
    }

    public static function factory() {
        if (!self::$_instance) {
            self::$_instance = new Mongo_Mysql();
        }
        return self::$_instance;
    }
	
	public static function findAllMatchByStatus($status,$sort='asc',$skip=0,$limit=0)
	{
		$arr = array("status" => (int)$status);
		if($sort == "asc") $sort = array("DateMatchTS" => 1); 
        if($sort == "desc") $sort = array("DateMatchTS" => -1);
		$arr_return = self::factory()->find("match", $arr)->sort($sort);
		if( $limit != 0)
		{
			$arr_return = $arr_return->skip($skip)->limit($limit);
		}
        return $arr_return;
	}
	
	public static function getAll($table,$condition,$sort_column,$sort='asc',$skip=0,$limit=0)
	{
		if( $sort_column )
		{
			if($sort == "asc") $sort = array($sort_column => 1); 
			if($sort == "desc") $sort = array($sort_column => -1);
			$arr_return = self::factory()->find($table, $condition)->sort($sort);
		}
		else
		{
			$arr_return = self::factory()->find($table, $condition);
		}
		if( $limit != 0)
		{
			$arr_return = $arr_return->skip($skip)->limit($limit);
		}
        return $arr_return;
	}
	
	public static function getOne($table,$condition)
	{	
        return self::factory()->find($table, $condition);
	}
	
	public static function onUpdate($table,$data,$id)
	{
		self::factory()->update($table, $data, $id);
	}
	
	public static function onInsert($table,$data)
	{
		self::factory()->insert($table, $data);
	}
	
	public static function onDelete($table,$id)
	{
		self::factory()->delete($table, $id);
	}
	
}

?>
